"""
RISC-V指令集定义

提供所有RISC-V基本指令的low-level API，每个API对应生成一条汇编指令。
"""

class InstructionSet:
    """RISC-V指令集类，提供所有基本指令的API"""
    
    def __init__(self, code_generator):
        self.code_gen = code_generator
    
    # ========== 算术指令 ==========
    
    def add(self, rd, rs1, rs2):
        """加法指令: rd = rs1 + rs2"""
        self.code_gen.emit(f"add {rd}, {rs1}, {rs2}")
        return rd
    
    def addi(self, rd, rs1, imm):
        """立即数加法指令: rd = rs1 + imm"""
        self.code_gen.emit(f"addi {rd}, {rs1}, {imm}")
        return rd
    
    def sub(self, rd, rs1, rs2):
        """减法指令: rd = rs1 - rs2"""
        self.code_gen.emit(f"sub {rd}, {rs1}, {rs2}")
        return rd
    
    def mul(self, rd, rs1, rs2):
        """乘法指令: rd = rs1 * rs2"""
        self.code_gen.emit(f"mul {rd}, {rs1}, {rs2}")
        return rd
    
    def div(self, rd, rs1, rs2):
        """除法指令: rd = rs1 / rs2"""
        self.code_gen.emit(f"div {rd}, {rs1}, {rs2}")
        return rd
    
    def rem(self, rd, rs1, rs2):
        """取余指令: rd = rs1 % rs2"""
        self.code_gen.emit(f"rem {rd}, {rs1}, {rs2}")
        return rd
    
    # ========== 逻辑指令 ==========
    
    def and_(self, rd, rs1, rs2):
        """与运算: rd = rs1 & rs2"""
        self.code_gen.emit(f"and {rd}, {rs1}, {rs2}")
        return rd
    
    def andi(self, rd, rs1, imm):
        """立即数与运算: rd = rs1 & imm"""
        self.code_gen.emit(f"andi {rd}, {rs1}, {imm}")
        return rd
    
    def or_(self, rd, rs1, rs2):
        """或运算: rd = rs1 | rs2"""
        self.code_gen.emit(f"or {rd}, {rs1}, {rs2}")
        return rd
    
    def ori(self, rd, rs1, imm):
        """立即数或运算: rd = rs1 | imm"""
        self.code_gen.emit(f"ori {rd}, {rs1}, {imm}")
        return rd
    
    def xor(self, rd, rs1, rs2):
        """异或运算: rd = rs1 ^ rs2"""
        self.code_gen.emit(f"xor {rd}, {rs1}, {rs2}")
        return rd
    
    def xori(self, rd, rs1, imm):
        """立即数异或运算: rd = rs1 ^ imm"""
        self.code_gen.emit(f"xori {rd}, {rs1}, {imm}")
        return rd
    
    # ========== 位移指令 ==========
    
    def sll(self, rd, rs1, rs2):
        """逻辑左移: rd = rs1 << rs2"""
        self.code_gen.emit(f"sll {rd}, {rs1}, {rs2}")
        return rd
    
    def slli(self, rd, rs1, shamt):
        """立即数逻辑左移: rd = rs1 << shamt"""
        self.code_gen.emit(f"slli {rd}, {rs1}, {shamt}")
        return rd
    
    def srl(self, rd, rs1, rs2):
        """逻辑右移: rd = rs1 >> rs2"""
        self.code_gen.emit(f"srl {rd}, {rs1}, {rs2}")
        return rd
    
    def srli(self, rd, rs1, shamt):
        """立即数逻辑右移: rd = rs1 >> shamt"""
        self.code_gen.emit(f"srli {rd}, {rs1}, {shamt}")
        return rd
    
    def sra(self, rd, rs1, rs2):
        """算术右移: rd = rs1 >> rs2 (符号位扩展)"""
        self.code_gen.emit(f"sra {rd}, {rs1}, {rs2}")
        return rd
    
    def srai(self, rd, rs1, shamt):
        """立即数算术右移: rd = rs1 >> shamt (符号位扩展)"""
        self.code_gen.emit(f"srai {rd}, {rs1}, {shamt}")
        return rd
    
    # ========== 比较指令 ==========
    
    def slt(self, rd, rs1, rs2):
        """小于比较: rd = (rs1 < rs2) ? 1 : 0"""
        self.code_gen.emit(f"slt {rd}, {rs1}, {rs2}")
        return rd
    
    def slti(self, rd, rs1, imm):
        """立即数小于比较: rd = (rs1 < imm) ? 1 : 0"""
        self.code_gen.emit(f"slti {rd}, {rs1}, {imm}")
        return rd
    
    def sltu(self, rd, rs1, rs2):
        """无符号小于比较: rd = (rs1 < rs2) ? 1 : 0 (unsigned)"""
        self.code_gen.emit(f"sltu {rd}, {rs1}, {rs2}")
        return rd
    
    def sltiu(self, rd, rs1, imm):
        """立即数无符号小于比较: rd = (rs1 < imm) ? 1 : 0 (unsigned)"""
        self.code_gen.emit(f"sltiu {rd}, {rs1}, {imm}")
        return rd
    
    # ========== 内存访问指令 ==========
    
    def lw(self, rd, offset, rs1):
        """加载字: rd = memory[rs1 + offset]"""
        self.code_gen.emit(f"lw {rd}, {offset}({rs1})")
        return rd
    
    def lh(self, rd, offset, rs1):
        """加载半字: rd = memory[rs1 + offset] (16-bit)"""
        self.code_gen.emit(f"lh {rd}, {offset}({rs1})")
        return rd
    
    def lb(self, rd, offset, rs1):
        """加载字节: rd = memory[rs1 + offset] (8-bit)"""
        self.code_gen.emit(f"lb {rd}, {offset}({rs1})")
        return rd
    
    def lhu(self, rd, offset, rs1):
        """加载无符号半字: rd = memory[rs1 + offset] (16-bit unsigned)"""
        self.code_gen.emit(f"lhu {rd}, {offset}({rs1})")
        return rd
    
    def lbu(self, rd, offset, rs1):
        """加载无符号字节: rd = memory[rs1 + offset] (8-bit unsigned)"""
        self.code_gen.emit(f"lbu {rd}, {offset}({rs1})")
        return rd
    
    def sw(self, rs2, offset, rs1):
        """存储字: memory[rs1 + offset] = rs2"""
        self.code_gen.emit(f"sw {rs2}, {offset}({rs1})")
    
    def sh(self, rs2, offset, rs1):
        """存储半字: memory[rs1 + offset] = rs2 (16-bit)"""
        self.code_gen.emit(f"sh {rs2}, {offset}({rs1})")
    
    def sb(self, rs2, offset, rs1):
        """存储字节: memory[rs1 + offset] = rs2 (8-bit)"""
        self.code_gen.emit(f"sb {rs2}, {offset}({rs1})")
    
    # ========== 分支指令 ==========
    
    def beq(self, rs1, rs2, label):
        """相等分支: if (rs1 == rs2) goto label"""
        self.code_gen.emit(f"beq {rs1}, {rs2}, {label}")
    
    def bne(self, rs1, rs2, label):
        """不等分支: if (rs1 != rs2) goto label"""
        self.code_gen.emit(f"bne {rs1}, {rs2}, {label}")
    
    def blt(self, rs1, rs2, label):
        """小于分支: if (rs1 < rs2) goto label"""
        self.code_gen.emit(f"blt {rs1}, {rs2}, {label}")
    
    def bge(self, rs1, rs2, label):
        """大于等于分支: if (rs1 >= rs2) goto label"""
        self.code_gen.emit(f"bge {rs1}, {rs2}, {label}")
    
    def bltu(self, rs1, rs2, label):
        """无符号小于分支: if (rs1 < rs2) goto label (unsigned)"""
        self.code_gen.emit(f"bltu {rs1}, {rs2}, {label}")
    
    def bgeu(self, rs1, rs2, label):
        """无符号大于等于分支: if (rs1 >= rs2) goto label (unsigned)"""
        self.code_gen.emit(f"bgeu {rs1}, {rs2}, {label}")
    
    def ble(self, rs1, rs2, label):
        """小于等于分支(伪指令): if (rs1 <= rs2) goto label"""
        self.code_gen.emit(f"ble {rs1}, {rs2}, {label}")
    
    def bgt(self, rs1, rs2, label):
        """大于分支(伪指令): if (rs1 > rs2) goto label"""
        self.code_gen.emit(f"bgt {rs1}, {rs2}, {label}")
    
    # ========== 跳转指令 ==========
    
    def jal(self, rd, label):
        """跳转并链接: rd = pc + 4; pc = label"""
        self.code_gen.emit(f"jal {rd}, {label}")
    
    def jalr(self, rd, rs1, offset=0):
        """寄存器跳转并链接: rd = pc + 4; pc = rs1 + offset"""
        self.code_gen.emit(f"jalr {rd}, {rs1}, {offset}")
    
    def j(self, label):
        """无条件跳转: pc = label"""
        self.code_gen.emit(f"j {label}")
    
    def jr(self, rs1):
        """寄存器跳转: pc = rs1"""
        self.code_gen.emit(f"jr {rs1}")
    
    def ret(self):
        """返回: pc = ra"""
        self.code_gen.emit("ret")
    
    # ========== 立即数加载指令 ==========
    
    def li(self, rd, imm):
        """加载立即数: rd = imm"""
        self.code_gen.emit(f"li {rd}, {imm}")
        return rd
    
    def lui(self, rd, imm):
        """加载高位立即数: rd = imm << 12"""
        self.code_gen.emit(f"lui {rd}, {imm}")
        return rd
    
    def auipc(self, rd, imm):
        """PC相对高位立即数: rd = pc + (imm << 12)"""
        self.code_gen.emit(f"auipc {rd}, {imm}")
        return rd
    
    # ========== 伪指令 ==========
    
    def mv(self, rd, rs1):
        """移动: rd = rs1"""
        self.code_gen.emit(f"mv {rd}, {rs1}")
        return rd
    
    def nop(self):
        """空操作"""
        self.code_gen.emit("nop")
    
    def la(self, rd, label):
        """加载地址: rd = &label"""
        self.code_gen.emit(f"la {rd}, {label}")
        return rd
    
    # ========== 系统调用指令 ==========
    
    def ecall(self):
        """环境调用(系统调用)"""
        self.code_gen.emit("ecall")
    
    def ebreak(self):
        """环境断点"""
        self.code_gen.emit("ebreak") 